﻿<?xml version="1.0" encoding="UTF-8" ?>
<?arg urn:xmd-environment=Eldorado.Metadata.XmdEnvironment, Eldorado.Metadata ?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:typing="urn:xmd-typing"
	xmlns:mapping="urn:xmd-mapping"
	xmlns:log="urn:log"
	xmlns:env="urn:xmd-environment">
	
<xsl:output method="text"/>

<DBInput/>

<xsl:template name="GetClassUUID">
	<xsl:param name="className"/>
	<xsl:choose>
		<UUIDInput/>
		<xsl:otherwise>
			<xsl:message terminate="yes">uuid for class <xsl:value-of select="$className"/> not defined</xsl:message>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--
<xsl:template match="@type[.='System.String']">
	<xsl:choose>
		<xsl:when test="../@length">varchar(<xsl:value-of select="../@length"/>) not null default ''</xsl:when>
		<xsl:otherwise>text</xsl:otherwise>
	</xsl:choose>
</xsl:template>
<xsl:template match="@type[.='System.Boolean']">bit not null default '0'</xsl:template>
<xsl:template match="@type[.='System.Int32']">int not null default '0'</xsl:template>
<xsl:template match="@type[.='System.DateTime']">datetime not null default '1800-01-01T00:00:00'</xsl:template>
<xsl:template match="@type[.='System.Decimal']">float not null default '0'</xsl:template>
<xsl:template match="@type[.='System.Double']">float not null default '0'</xsl:template>
<xsl:template match="@type[.='System.Single']">float not null default '0'</xsl:template>
<xsl:template match="@type[.='System.Byte[]']">image</xsl:template>


<xsl:template match="@type">
	<xsl:message terminate="yes">datatype <xsl:value-of select="."/> not defined</xsl:message>
</xsl:template>-->

	
<xsl:template match="/">---- File: Delete.Satori.<xsl:value-of select="local-name(/*[1])"/>.sql
	<xsl:variable name="entities" select="*/*[typing:entity[not(@abstract='true')]]"/>
	<xsl:variable name="references" select="//typing:reference"/>
	<xsl:variable name="SetTotal" select="env:SetProgressTotal(count($entities) * 3)" xmlns:env="urn:xmd-environment" />
	<xsl:apply-templates select="$entities" mode="delete"/>
	<xsl:apply-templates select="$references" mode="delete"/>
	
---- File: Create.Satori.<xsl:value-of select="local-name(/*[1])"/>.sql
	
	<xsl:apply-templates  select="$entities" mode="create"/>
	<xsl:apply-templates select="$references" mode="create"/>

	<xsl:apply-templates select="$entities" mode="insert"/>
	
---- File: Index.Satori.<xsl:value-of select="local-name(/*[1])"/>.sql
	<xsl:apply-templates  select="$entities" mode="index"/>	
</xsl:template>

<xsl:template match="*[typing:entity]" mode="delete">
<!--IF not (OBJECT_ID('dbo.<xsl:value-of select="env:TypeName(typing:entity/@type)"/>', 'U') IS NULL)
	drop table [dbo].[<xsl:value-of select="env:TypeName(typing:entity/@type)"/>] 
GO-->
</xsl:template>

<xsl:template match="*[typing:entity]" mode="create">



<xsl:variable name="className" select="env:TypeName(typing:entity/@type)" xmlns:env="urn:xmd-environment"/>
<xsl:variable name="classId"><xsl:call-template name="GetClassUUID"><xsl:with-param name="className" select="$className"/></xsl:call-template></xsl:variable>
<xsl:variable name="log" select="env:Log(concat('Processing type ', className),'0')" xmlns:env="urn:xmd-environment"/>
<xsl:variable name="progress" select="env:UpdateProgress()" xmlns:env="urn:xmd-environment"/>
<!--<xsl:message>entity <xsl:value-of select="className"/></xsl:message>-->

<xsl:if test="not(typing:entity/@abstract='true')">
<!--<xsl:for-each select="*[typing:property | typing:reference[@multiplicity='1']]"><xsl:apply-templates select="." mode="column"/>
	</xsl:for-each>,

GO
-->
</xsl:if>
</xsl:template>

<xsl:template match="*[typing:entity]" mode="delete">



<xsl:variable name="className" select="env:TypeName(typing:entity/@type)" xmlns:env="urn:xmd-environment"/>
<xsl:variable name="classId"><xsl:call-template name="GetClassUUID"><xsl:with-param name="className" select="$className"/></xsl:call-template></xsl:variable>
<xsl:variable name="log" select="env:Log(concat('Processing type ', className),'0')" xmlns:env="urn:xmd-environment"/>
<xsl:variable name="progress" select="env:UpdateProgress()" xmlns:env="urn:xmd-environment"/>
<!--<xsl:message>entity <xsl:value-of select="className"/></xsl:message>-->

<xsl:if test="not(typing:entity/@abstract='true')">

</xsl:if>
</xsl:template>

<xsl:template match="*[typing:entity]" mode="index">
<xsl:if test="not(typing:entity/@abstract='true')">
<xsl:for-each select="*[typing:property | typing:reference[@multiplicity='1']]"><xsl:apply-templates select="." mode="indexcolumn"/></xsl:for-each>
</xsl:if>
</xsl:template>

<xsl:template match="*[typing:entity]" mode="insert">
<xsl:variable name="className" select="env:TypeName(typing:entity/@type)" xmlns:env="urn:xmd-environment"/>
<xsl:variable name="classId"><xsl:call-template name="GetClassUUID"><xsl:with-param name="className" select="$className"/></xsl:call-template></xsl:variable>

</xsl:template>

<xsl:template match="typing:property" mode="column">
</xsl:template>
						
<xsl:template match="typing:reference[@multiplicity='1' and @adjacentMultiplicity = '1']" mode="column">
	<xsl:variable name="back-role"><xsl:choose><xsl:when test="@parent"><xsl:value-of select="@parent"/></xsl:when><xsl:otherwise><xsl:value-of select="local-name(..)"/></xsl:otherwise></xsl:choose></xsl:variable>
	<xsl:variable name="tableName" select="env:TableName($back-role, local-name(..))"/>
	
	<!--<xsl:message>db </xsl:message>
	<xsl:message>parent <xsl:value-of select="@parent"/> </xsl:message>
	<xsl:message>table name = <xsl:value-of select="$tableName"/> </xsl:message>
	<xsl:message>back-role = <xsl:value-of select="$back-role"/> </xsl:message>
	<xsl:message>local-name = <xsl:value-of select="local-name(..)"/> </xsl:message>-->
	
	<xsl:if test="@navigable = 'false' or boolean(starts-with($tableName, local-name(..)))">	
	<!--<xsl:message>boolean(starts-with($tableName, local-name())) ok </xsl:message>-->
	<xsl:variable name="name"><xsl:value-of select="local-name(..)"/></xsl:variable> 
	if not exists (SELECT  'Yes' AS Ex
FROM INFORMATION_SCHEMA.COLUMNS
WHERE   TABLE_NAME = '<xsl:value-of select="$tableName"/>'
AND  COLUMN_NAME = '<xsl:value-of select="$name"/>')
	alter table [<xsl:value-of select="$tableName"/>] add [<xsl:value-of select="$name"/>] uniqueidentifier


	<!--<xsl:variable name="name"><xsl:value-of select="local-name(..)"/></xsl:variable> 
	,[<xsl:value-of select="$name"/>] uniqueidentifier--></xsl:if>
	</xsl:template>
	
<xsl:template match="typing:reference[@multiplicity='1' and @adjacentMultiplicity != '1']" mode="column">
	<!--<xsl:variable name="name"><xsl:value-of select="local-name(..)"/></xsl:variable> 
	,[<xsl:value-of select="$name"/>] uniqueidentifier--></xsl:template>

<xsl:template match="typing:property" mode="indexcolumn">
	<xsl:variable name="name"><xsl:value-of select="local-name(..)"/></xsl:variable>	
	<xsl:variable name="type"><xsl:apply-templates select="@type"/></xsl:variable>
	
	if not exists (SELECT  'Yes' AS Ex
FROM INFORMATION_SCHEMA.COLUMNS
WHERE   TABLE_NAME = '<xsl:value-of select="env:TypeName(../../typing:entity/@type)"/>'
AND  COLUMN_NAME = '<xsl:value-of select="$name"/>')
	alter table [<xsl:value-of select="env:TypeName(../../typing:entity/@type)"/>] add [<xsl:value-of select="$name"/>] <xsl:value-of select="$type"/>
<!--
	if not exists (select * from sysindexes
  where id=object_id('<xsl:value-of select="env:TypeName(../../typing:entity/@type)"/>') 
    and name='<xsl:value-of select="$name"/>')
    CREATE INDEX [<xsl:value-of select="$name"/>] ON [<xsl:value-of select="env:TypeName(../../typing:entity/@type)"/>] ([<xsl:value-of select="$name"/>])
-->
</xsl:template>
 

<xsl:template match="typing:reference[@multiplicity='1' and @adjacentMultiplicity = '1']" mode="indexcolumn">
	<xsl:variable name="back-role"><xsl:choose><xsl:when test="@parent"><xsl:value-of select="@parent"/></xsl:when><xsl:otherwise><xsl:value-of select="local-name(..)"/></xsl:otherwise></xsl:choose></xsl:variable>
	<xsl:variable name="tableName" select="env:TableName($back-role, local-name(..))"/>
	<xsl:variable name="type"><xsl:apply-templates select="@type"/></xsl:variable>
	<xsl:if test="boolean(starts-with($tableName, local-name(..)))">
	<xsl:variable name="name"><xsl:value-of select="local-name(..)"/></xsl:variable> 
	if not exists (SELECT  'Yes' AS Ex
FROM INFORMATION_SCHEMA.COLUMNS
WHERE   TABLE_NAME = '<xsl:value-of select="$tableName"/>'
AND  COLUMN_NAME = '<xsl:value-of select="$name"/>')
	alter table [<xsl:value-of select="$tableName"/>] add [<xsl:value-of select="$name"/>] uniqueidentifier
	<!--if not exists (select * from sysindexes
  where id=object_id('<xsl:value-of select="env:TypeName(../../typing:entity/@type)"/>') 
    and name='<xsl:value-of select="$name"/>')
    CREATE INDEX [<xsl:value-of select="$name"/>] ON [<xsl:value-of select="env:TypeName(../../typing:entity/@type)"/>] ([<xsl:value-of select="$name"/>])-->
	 </xsl:if></xsl:template>
	
<xsl:template match="typing:reference[@multiplicity='1' and @adjacentMultiplicity != '1']" mode="indexcolumn">
	<xsl:variable name="name"><xsl:value-of select="local-name(..)"/></xsl:variable> 
	<xsl:variable name="type"><xsl:apply-templates select="@type"/></xsl:variable>
	if not exists (SELECT  'Yes' AS Ex
FROM INFORMATION_SCHEMA.COLUMNS
WHERE   TABLE_NAME = '<xsl:value-of select="env:TypeName(../../typing:entity/@type)"/>'
AND  COLUMN_NAME = '<xsl:value-of select="$name"/>')
	alter table [<xsl:value-of select="env:TypeName(../../typing:entity/@type)"/>] add [<xsl:value-of select="$name"/>] uniqueidentifier
	<!--
	if not exists (select * from sysindexes
  where id=object_id('<xsl:value-of select="env:TypeName(../../typing:entity/@type)"/>') 
    and name='<xsl:value-of select="$name"/>')
    CREATE INDEX [<xsl:value-of select="$name"/>] ON [<xsl:value-of select="env:TypeName(../../typing:entity/@type)"/>] ([<xsl:value-of select="$name"/>])-->
    </xsl:template>


<xsl:template match="typing:reference[@multiplicity='*' and (@navigable = 'false' or @adjacentMultiplicity = '*')]" mode="create">
	<xsl:variable name="back-role"><xsl:choose><xsl:when test="@child"><xsl:value-of select="@child"/></xsl:when><xsl:otherwise><xsl:value-of select="local-name(../..)"/></xsl:otherwise></xsl:choose></xsl:variable>
	<xsl:variable name="tableName" select="env:TableName($back-role, local-name(..))"/>	
	<!--
IF NOT EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '<xsl:value-of select="$tableName"/>')

CREATE TABLE [dbo].[<xsl:value-of select="$tableName"/>] (
	[<xsl:value-of select="$back-role"/>] uniqueidentifier,
	[<xsl:value-of select="local-name(..)"/>] uniqueidentifier
	,[modifiele] datetime not null default '1800-01-01T00:00:00'
	,[modifiepar] varchar(30) not null default ''
	,PRIMARY KEY ([<xsl:value-of select="$back-role"/>], [<xsl:value-of select="local-name(..)"/>])
)

GO-->
</xsl:template>

<xsl:template match="typing:reference[@multiplicity='*' and (@navigable = 'false' or @adjacentMultiplicity = '*')]" mode="delete">
	<xsl:variable name="back-role"><xsl:choose><xsl:when test="@child"><xsl:value-of select="@child"/></xsl:when><xsl:otherwise><xsl:value-of select="local-name(../..)"/></xsl:otherwise></xsl:choose></xsl:variable>
	<xsl:variable name="tableName" select="env:TableName($back-role, local-name(..))"/>	
	<!--
IF not (OBJECT_ID('dbo.<xsl:value-of select="$tableName"/>', 'U') IS NULL)
	drop table [dbo].[<xsl:value-of select="$tableName"/>] 
GO-->
</xsl:template>


</xsl:stylesheet>